package com.gxuwz.business.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.gxuwz.business.entity.SysUser;
import com.gxuwz.business.service.ISysUserService;
import com.gxuwz.util.Result;
import com.gxuwz.util.SystemContext;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

import net.sf.json.JSONObject;


public class SysUserAction extends BaseAction implements Preparable,ModelDriven<SysUser>{
	
	private static final String UPDATE_PASSWORD = "/WEB-INF/page/sysUser/sysUser_updatePassword.jsp";
	private static final String UPDATE_INFO = "/WEB-INF/page/sysUser/sysUser_updateInfo.jsp";
	
	@Autowired
	private ISysUserService sysUserService;
	private SysUser sysUser;
	private SysUser user = new SysUser();
	private String newUserPassword;
	String errorMessage = "";
	private Result result;
	private String provid;//省
	private String cityid;//市
	private String areaid;//区
	
	private File file; //头像图片
	private String fileContentType; //文件类型
	private String fileFileName; //文件名
	
	/**
	 * 打开修改密码界面
	 */
	public String openUpdatePassword(){
		//获取用户
		user = (SysUser)getSession().getAttribute("user");
		System.out.println("sysUser"+sysUser);
		forwardView =  UPDATE_PASSWORD;
		return SUCCESS;
	}
	
	/**
	 * 打开修改个人信息界面
	 */
	public String openUpdateInfo(){
		user = (SysUser) getSession().getAttribute("user");
		forwardView = UPDATE_INFO;
		return SUCCESS;
	}
	
	/**
	 * 头像预览
	 * @throws IOException 
	 */
	public void userFecePreview() throws IOException{
		if(fileFileName == null || fileFileName.equals("")){
			//默认头像
			fileFileName = SystemContext.USER_FACE;
		}
		String filePath = getSession().getServletContext().getRealPath(fileFileName);
		//读取本地图片输入流
		FileInputStream inputStream = new FileInputStream(filePath);
		int i = inputStream.available();
		//byte数组存放图片字节数组
		byte[] buff = new byte[i];
		inputStream.read(buff);
		//关闭输入流
		inputStream.close();
		//设置发送到客户端的相应内容类型
		getResponse().setContentType("image/*");
		OutputStream out = getResponse().getOutputStream();
		out.write(buff);
		//关闭响应输出流
		out.close();
	}
	
	/**
	 * 上传头像
	 */
	public String uploadUserFace() throws Exception{
		user = sysUserService.findById(sysUser.getUserId());
//		System.out.println(file);
//		System.out.println(fileContentType);
//		System.out.println(fileFileName);
		String ex = fileFileName.substring(fileFileName.lastIndexOf(".")+1); //文件扩展名
//		System.out.println(ex);
		String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
		String secondDate = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
		String savePath = getSession().getServletContext().getRealPath(SystemContext.IMG)+"/"+date+"/";
		System.out.println("savePath: "+savePath);
		String url = SystemContext.IMG+date+"/";
//		System.out.println("url: "+url);
		String newFileName = secondDate+ new Random().nextInt(1000)+"."+ex;  //文件名
//		System.out.println("newFileName: "+newFileName);
		//创建目录和文件
		File uploadFile = new File(savePath+newFileName);
		if(!uploadFile.getParentFile().exists()){
			uploadFile.getParentFile().mkdirs();
		}
		try {
			uploadFile.createNewFile();
			FileUtils.copyFile(file, uploadFile);
		} catch (Exception e) {
			e.printStackTrace();
		}
		//返回文件url
		user.setUserImg(url+newFileName);
		result = sysUserService.uploadUserFace(user);
		HashMap<String, String> data = new HashMap<String, String>();
		data.put("src", url+newFileName);
		result.setData(data);
		JsonData = JSONObject.fromObject(result);
//		System.out.println(JsonData);
//		System.out.println(result);
		System.out.println(url + newFileName);
		return "JsonObject";
	}
	
	/**
	 * 更新用户信息
	 * @throws Exception 
	 */
	public String editInfomation() throws Exception{
		System.out.println(provid+" "+cityid+" "+areaid);
		user = sysUserService.findById(sysUser.getUserId());
		user.setUserRealName(sysUser.getUserRealName());
		user.setUserAge(sysUser.getUserAge());
		user.setUserSex(sysUser.getUserSex());
		user.setUserTelephone(sysUser.getUserTelephone());
		user.setUserAddress(sysUser.getUserAddress());
		user.setUserEducation(sysUser.getUserEducation());
		user.setUserQq(sysUser.getUserQq());
		user.setUserWx(sysUser.getUserWx());
		isSucceed = sysUserService.update(user);
		jsonString = jsonToString(isSucceed);
		return "JsonString";
	}
	
	/**
	 * 修改用户名和密码
	 */
	public String editPassword(){
		System.out.println("新密码："+newUserPassword);
		String newUserPassword1 = SystemContext.passwdEncryption(newUserPassword);
		if(sysUserService.isTrue(sysUser)){
			isSucceed = sysUserService.updatePassword(newUserPassword1,sysUser);
			System.out.println(isSucceed);
			jsonString = jsonToString(isSucceed);
		}else{
			isSucceed = false;
			jsonString = jsonToString(isSucceed);
		}
		
		return "JsonString";
	}
	


	public String getNewUserPassword() {
		return newUserPassword;
	}

	public void setNewUserPassword(String newUserPassword) {
		this.newUserPassword = newUserPassword;
	}

	@Override
	public SysUser getModel() {
		return sysUser;
	}

	@Override
	public void prepare() throws Exception {
		if(null == sysUser){
			sysUser = new SysUser();
		}
	}

	public SysUser getUser() {
		return user;
	}

	public void setUser(SysUser user) {
		this.user = user;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public String getProvid() {
		return provid;
	}

	public void setProvid(String provid) {
		this.provid = provid;
	}

	public String getCityid() {
		return cityid;
	}

	public void setCityid(String cityid) {
		this.cityid = cityid;
	}

	public String getAreaid() {
		return areaid;
	}

	public void setAreaid(String areaid) {
		this.areaid = areaid;
	}


	

	
}
